package xgenerator.ui.swing.adapter;

import java.awt.Component;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import xgenerator.ui.swing.MetadataTable;
import xgenerator.ui.swing.model.MetadataTableModel;

public class MetadataTableRowAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 复制注释为描述
	 */
	public static final String COPY_COMMENT_TO_DISPLAY_NAME = "COPY_COMMENT_TO_DISPLAY_NAME";

	/**
	 * 复制全部注释为描述
	 */
	public static final String COPY_ALL_COMMENT_TO_DISPLAY_NAME = "COPY_ALL_COMMENT_TO_DISPLAY_NAME";

	/**
	 * 在当前行上方插入行actionCommand
	 */
	public static final String INSERT_ROW_ABOVE_CURRENT_ROW_COMMAND = "INSERT_ROW_ABOVE_CURRENT_ROW_COMMAND";

	/**
	 * 在当前行下方插入行actionCommand
	 */
	public static final String INSERT_ROW_BELOW_CURRENT_ROW_COMMAND = "INSERT_ROW_BELOW_CURRENT_ROW_COMMAND";

	/**
	 * 在底部插入行actionCommand
	 */
	public static final String INSERT_ROW_AT_BOTTOM_COMMAND = "INSERT_ROW_AT_BOTTOM_COMMAND";

	/**
	 * 删除行actionCommand
	 */
	public static final String DELETE_ROW_COMMAND = "DELETE_ROW_COMMAND";

	/**
	 * UI组件
	 */
	private Component ui;

	/**
	 * constructor
	 * @param ui
	 */
	public MetadataTableRowAction(Component ui) {
		this.ui = ui;
	}

	/**
	 * <p>
	 * Title：
	 * </p>
	 * <p>
	 * Description：
	 * </p>
	 * 
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 * @param e
	 */
	public void actionPerformed(ActionEvent e) {
		if (COPY_COMMENT_TO_DISPLAY_NAME.equals(e.getActionCommand())) {
			copyComment2DescriptioinActionPerformed(e);
		} else if (COPY_ALL_COMMENT_TO_DISPLAY_NAME.equals(e.getActionCommand())) {
			copyAllComment2DescriptioinsActionPerformed(e);
		} else if (INSERT_ROW_ABOVE_CURRENT_ROW_COMMAND.equals(e.getActionCommand())) {
			insertRowAboveCurrentRowActionPerformed(e);
		} else if (INSERT_ROW_BELOW_CURRENT_ROW_COMMAND.equals(e.getActionCommand())) {
			insertRowBelowCurrentRowActionPerformed(e);
		} else if (INSERT_ROW_AT_BOTTOM_COMMAND.equals(e.getActionCommand())) {
			insertRowAtBottomActionPerformed(e);
		} else if (DELETE_ROW_COMMAND.equals(e.getActionCommand())) {
			deleteRowActionPerformed(e);
		}
	}
	
	/**
	 * <p>
	 * Title：功能方法名
	 * </p>
	 * @param e
	 */
	public void copyComment2DescriptioinActionPerformed(ActionEvent e) {
		MetadataTable metadataTable = (MetadataTable) ui;
		MetadataTableModel metadataTableModel = (MetadataTableModel) metadataTable.getModel();
		int selectedRow = metadataTable.getSelectedRow();
		
		Object comments = metadataTableModel.getValueAt(selectedRow, MetadataTableModel.COMMENTS_INDEX);
		
		metadataTableModel.setValueAt(comments, selectedRow, MetadataTableModel.DISPLAY_NAME_INDEX);
		metadataTable.revalidate();
		metadataTable.updateUI();
	}
	
	/**
	 * <p>
	 * Title：功能方法名
	 * </p>
	 * @param e
	 */
	public void copyAllComment2DescriptioinsActionPerformed(ActionEvent e) {
		MetadataTable metadataTable = (MetadataTable) ui;
		MetadataTableModel metadataTableModel = (MetadataTableModel) metadataTable.getModel();

		for (int i = 0; i < metadataTableModel.getRowCount(); i++) {
			Object comments = metadataTableModel.getValueAt(i, MetadataTableModel.COMMENTS_INDEX);
			metadataTableModel.setValueAt(comments, i, MetadataTableModel.DISPLAY_NAME_INDEX);
		}

		metadataTable.revalidate();
		metadataTable.updateUI();
	}
	
	/**
	 * <p>
	 * Title：在上行插入行
	 * </p>
	 * @param e
	 */
	public void insertRowAboveCurrentRowActionPerformed(ActionEvent e) {
		try {
			MetadataTable metadataTable = (MetadataTable) ui;
			MetadataTableModel metadataTableModel = (MetadataTableModel) metadataTable.getModel();
			int selectedRow = metadataTable.getSelectedRow();
			
			int rowAbove = selectedRow;
			//metadataTableModel.insertRow(rowAbove, metadataTableModel.mapRowData(tableColumnMetadataBean));
			//metadataTableModel.insertRowEditable(rowAbove);
			metadataTableModel.insertNewRow(rowAbove);
			metadataTable.revalidate();
			metadataTable.updateUI();
		} catch (Exception ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(ui, "插入行失败！");
		}
	}
	
	/**
	 * <p>
	 * Title：在下行插入行
	 * </p>
	 * @param e
	 */
	public void insertRowBelowCurrentRowActionPerformed(ActionEvent e) {
		try {
			MetadataTable metadataTable = (MetadataTable) ui;
			MetadataTableModel metadataTableModel = (MetadataTableModel) metadataTable.getModel();
			int selectedRow = metadataTable.getSelectedRow();
			
			int rowBelow = selectedRow + 1;
			//metadataTableModel.insertRow(rowBelowIndex, metadataTableModel.mapRowData(tableColumnMetadataBean));
			//metadataTableModel.insertRowEditable(rowBelowIndex);
			metadataTableModel.insertNewRow(rowBelow);
			metadataTable.revalidate();
			metadataTable.updateUI();
		} catch (Exception ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(ui, "插入行失败！");
		}
	}
	
	/**
	 * <p>
	 * Title：在底部插入行
	 * </p>
	 * @param e
	 */
	public void insertRowAtBottomActionPerformed(ActionEvent e) {
		try {
			MetadataTable metadataTable = (MetadataTable) ui;
			MetadataTableModel metadataTableModel = (MetadataTableModel) metadataTable.getModel();
			
			int lastRow = metadataTable.getRowCount();
			//metadataTableModel.insertRow(lastRowIndex, metadataTableModel.mapRow(tableColumnMetadataBean));
			//metadataTableModel.addRow(metadataTableModel.mapRowData(tableColumnMetadataBean));
			//metadataTableModel.insertRowEditable(lastRowIndex);
			metadataTableModel.insertNewRow(lastRow);
			metadataTable.revalidate();
			metadataTable.updateUI();
		} catch (Exception ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(ui, "插入行失败！");
		}
	}
	
	/**
	 * <p>
	 * Title：删除行
	 * </p>
	 * @param e
	 */
	public void deleteRowActionPerformed(ActionEvent e) {
		try {
			MetadataTable metadataTable = (MetadataTable) ui;
			int selectedRow = metadataTable.getSelectedRow();
			MetadataTableModel metadataTableModel = (MetadataTableModel) metadataTable.getModel();
			
			//metadataTableModel.removeRow(selectedRow);
			//metadataTableModel.removeRowEditable(selectedRow);
			//metadataTableModel.removeOneRow(selectedRow);
			metadataTableModel.removeRow(selectedRow);
			metadataTable.revalidate();
			metadataTable.updateUI();
			JOptionPane.showMessageDialog(ui, "删除行成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(ui, "删除行失败！");
		}
	}

	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	public Component getUI() {
		return ui;
	}

	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param ui
	 */
	public void setUI(Component ui) {
		this.ui = ui;
	}
	
}
